'''
Created on 2015-8-23

@author: yuanbin.myb
'''

import urllib
import lxml.html.soupparser as soupparser
import sys
sys.path.append("../pojo")
import StockHolder

class PageAnaly:
    
    def __init__(self):
        self.stockCode="";
        self.preUrl="http://f10.eastmoney.com/f10_v2/ShareholderResearch.aspx?code=";
        self.stockHolderNum=10;
        self.xpathString = "//div[@id='TTCS_Table_Div']/table[1]/tr[%d]/td[%d]";
        self.stockHolder=[];
        
    def setStockCode(self,code):
        self.stockCode= code;
        
    def getStockCode(self):
        return self.stockCode;
    
    def getHtmlSource(self):
        url = self.preUrl+self.stockCode;
        sock = urllib.urlopen(url);
        htmlSource = sock.read()
        sock.close()
        return htmlSource        
     
    def getStockHolderList(self):
        htmlSource=PageAnaly.getHtmlSource(self);
        dom = soupparser.fromstring(htmlSource)
        
        #print dom.xpath("//div[@id='TTCS_Table_Div']/table[1]/tr[2]/td[1]")[0].text
        #sys.exit()
        for i in range(10):
            #print "i:",i;
            stockHolder=StockHolder.StockHolder();
            for j in range(7):
                #print "j:",j
                xpathString = self.xpathString%(i+2,j+1);
                #print "xpathString:",xpathString;
                if(1 == j+1):
                    stockHolder.setName(dom.xpath(xpathString)[0].text)
                    #print "StockName:%s"%dom.xpath(xpathString)[0].text
                if(2 == j+1):
                    stockHolder.setProperty(dom.xpath(xpathString)[0].text)
                if(3 == j+1):
                    stockHolder.setType(dom.xpath(xpathString)[0].text)
                if(4 == j+1):
                    stockHolder.setStockNum(dom.xpath(xpathString)[0].text.replace(',',''))
                if(5 == j+1):
                    stockHolder.setRate(dom.xpath(xpathString)[0].text)
                if(6 == j+1):
                    stockHolder.setUpAndDown(dom.xpath(xpathString)[0].text)
                if(7 == j+1):
                    stockHolder.setAlterRate(dom.xpath(xpathString)[0].text)
            self.stockHolder.append(stockHolder.toList());
            #print "getName:%s"%stockHolder.getName()
            #print "getName:%s"%stockHolder.getProperty()
            stockHolder.reset();    
        return  self.stockHolder   


  
if __name__ == '__main__':
    
        pageAnaly = PageAnaly()
        pageAnaly.setStockCode("SH600601")
        list=pageAnaly.getStockHolderList()
        print "len:%d"%len(list) 
        print "Name:%s"%list[0]
        print "Name:%s"%list[1]
        print "Name:%s"%list[2]
        print "Type:%s"%type(list[0]) 
        